package com.ctgu.fastleader.controller;

import com.ctgu.fastleader.service.DeliveryService;
import com.ctgu.fastleader.service.OutputOrderService;
import com.ctgu.fastleader.vo.Delivery;
import com.ctgu.fastleader.vo.OutputOrder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/delivery")
public class DeliveryController {

    @Resource
    DeliveryService deliveryService;

    @Resource
    OutputOrderService orderService;

    @RequestMapping("/toSendGoods")
    public String toSendGoods(Integer orderid, Model model){
        model.addAttribute("orderid",orderid);
        return "forward:/page/toDetailSendGoods";
    }

    @RequestMapping("/sendGoods")
    public String sendGoods(HttpServletRequest request){
        Integer orderid = Integer.valueOf(request.getParameter("orderid"));
        Integer deliveryid = Integer.valueOf(request.getParameter("deliveryman"));
        Delivery delivery = new Delivery(orderid,deliveryid);
        deliveryService.insertSelective(delivery);
        Integer state = 2;
        OutputOrder outputOrder = new OutputOrder(orderid,state);
        orderService.updateByPrimaryKeySelective(outputOrder);
        return "forward:/page/toDelivery";
    }

    @ResponseBody
    @RequestMapping("/SendGoodsRecord")
    public List<Delivery> SendGoodsRecord(){
        return deliveryService.selectSendGoodsRecord();
    }
}
